<template>
  <!--审核课题（学院教务员）-->
  <div class="review-topic">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--指定审核人 弹框-->
      <DesignatedDialog ref="designatedDialog" :designatedDialogVisible="dialogVisible" :selectedList="subjectSelectedData" @designated-submit="designatedSubmit" @designated-cancel="designatedCancel"></DesignatedDialog>

      <condition-card @reset="reset" @search="searchList" label-width="140px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.college')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.optMajor')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectType')">
              <el-select v-model="queryForm.projectType" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectNature')">
              <el-select v-model="queryForm.projectNatureId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectNatureOptions" :key="item.id" :label="item.subjectProperties" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectSource')">
              <el-select v-model="queryForm.projectSourceId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectSourceOptions" :key="item.id" :label="item.subjectSource" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectName')">
              <el-input v-model="queryForm.projectName" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.declarer')">
              <el-input v-model="queryForm.applicantId" class="search-item" placeholder="请输入申报人姓名或工号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.socialPractice')">
              <el-select v-model="queryForm.practiceSocialId" filterable class="search-item" clearable>
                <el-option v-for="item in source.socialPracticeOptions" :key="item.id" :label="item.sociaPractice" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.reviewStatus')">
              <el-select v-model="queryForm.status" filterable class="search-item" clearable>
                <el-option v-for="item in source.statusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.reviewSubject')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
            <el-button type="primary" @click="onDesignated" v-premit="designated_reviewer">{{$t('graduationDesign.designatedReviewer')}}</el-button>
          </div>
        </div>

        <edu-table ref="reviewSubjectTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.declarer')" prop="applicantName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectCollege')" prop="collegeIdI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectMajor')" prop="subjectIdI18n" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectDerection')" prop="directionName" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectSource')" prop="projectSource" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectNature')" prop="projectNature" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.socialPractice')" prop="practiceSocial" min-width="140"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.reviewer')" prop="approvalName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.status')" min-width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'WaitCommit'" type="info">{{$t('graduationDesign.draft')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Process'" type="info">{{$t('graduationDesign.pendingReview')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">{{$t('graduationDesign.rejected')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'End'" type="success">{{$t('graduationDesign.passed')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'VIEW'">
      <!--查看-->
      <TopicInfo :actionType="currentAction" :basicInfoForm="basicInfoForm" :progressData="progressData"></TopicInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/reviewSubject"
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'reviewSubjectAcademic',
  data() {
    return {
      api: API,
      getList: '/api/designservice/optApproval/list',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectName: '',
        applicantId: '',
        status: '',
        authId: localStorage.getItem('activeAuthId'),
        practiceSocialId: ''
      },
      loading: false,
      indexKey: 'MAIN',
      source: {
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        // 课题类型
        subjectTypeOptions: [{
          label: "毕业论文",
          value: "毕业论文",
        }, {
          label: "毕业设计",
          value: "毕业设计",
        }],
        subjectNatureOptions: [], // 课题性质
        subjectSourceOptions: [], // 课题来源
        socialPracticeOptions: [], // 其中社会实践情况
        // 审核状态
        statusOptions: [{
          label: "草稿",
          value: "WaitCommit"
        }, {
          label: "待审核",
          value: "Process"
        }, {
          label: "已驳回",
          value: "Reject"
        }, {
          label: "已通过",
          value: "End"
        }]
      },
      breadPath: {
        label: this.$t('graduationDesign.reviewSubject'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.details')
          },
          {
            key: "REVIEW",
            label: this.$t('graduationDesign.reviewTopic')
          }
        ]
      },
      subjectSelectedData: [], // 勾选的列表数据
      dialogVisible: false,
      currentAction: "",
      rejectForm: {},
      rejectRules: {
        remark: [
          { required: true, message: this.$t('graduationDesign.pleaseEnter'), trigger: "blur" }
        ]
      },
      basicInfoForm: {},
      progressData: [] //审核进度列表
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + '';
    this.getCollege();
    this.getSubjectSource();
    this.getSubjectNature();
    this.getSocialPractice();
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.reviewSubjectTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectName: '',
        applicantId: '',
        status: '',
        authId: localStorage.getItem('activeAuthId')
      };
      this.source.subjectOptions = [];
      this.queryForm.grade = new Date().getFullYear() + '';
      this.searchList();
      this.getSubjectSource();
      this.getSubjectNature();
      this.getSocialPractice();
    },
    handleSelectChange(val) {
      this.subjectSelectedData = val;
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
      this.getSubjectSource();
      this.getSubjectNature();
      this.getSocialPractice();
      this.searchList();
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查课题来源
    getSubjectSource() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getSource(params).then(res => {
        this.source.subjectSourceOptions = res.data;
      }).catch();
    },
    // 查课题性质
    getSubjectNature() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getNature(params).then(res => {
        this.source.subjectNatureOptions = res.data;
      }).catch();
    },
    // 查社会实践
    getSocialPractice() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getPractice(params).then(res => {
        this.source.socialPracticeOptions = res.data;
      }).catch();
    },
    // 指定审核人
    onDesignated() {
      if (!this.subjectSelectedData.length) {
        this.$eduAlert(this.$t('graduationDesign.pleaseSelectData'), this.$t('graduationDesign.prompt'));
        return false;
      }
      if(this.subjectSelectedData.some(item => item.status !== 'Process')) {
        this.$eduAlert('只允许对待审核的数据指定审核人', this.$t('graduationDesign.prompt'));
        return;
      }
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.designatedDialog.reset();
      });
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 查看
    onView(row) {
      this.currentAction = "VIEW_STU";
      // "infoType"： 1 老师，0 学生
      if (row.infoType === "1") {
        this.currentAction = "VIEW_TUTOR";
      }

      const params = { ...row };
      this.api.getSubjectInfo(params).then(res => {
        this.indexKey = 'VIEW';
        this.basicInfoForm = { ...res.data };
        this.basicInfoForm.grade = res.data.grade + "";
        this.progressData = [];
        if (row.flowNo) {
          this.getReviewProgress(row.flowNo);
        }
      }).catch();
    },
    // 指定审核人 提交
    designatedSubmit(data) {
      this.api.designated(data).then(res => {
        this.$message.success(this.$t('graduationDesign.submitSuccess'));
        this.dialogVisible = false;
        this.searchList();
      }).catch();
    },
    // 指定审核人 取消
    designatedCancel() {
      this.dialogVisible = false;
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.subjectSelectedData });
      this.api.academicExport(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "审核课题(学院教务员).xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    DesignatedDialog: () => import('./components/designatedDialog'),
    TopicInfo: () => import('../components/topicInfo')
  }
}
</script>
<style lang="scss" scoped>
.review-topic {
    .search-item {
        width: 100%;
    }
}
</style>
